package main.java;

/**
 * @author: 大怪
 * @email: 962527441@qq.com
 */
public class Test83_合并链表并返回范围和 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    public int execute(ListNode listNode1, ListNode listNode2, int startIndex, int endIndex) {
        // write code here
        ListNode listNode = mergeList(listNode1, listNode2);
        for (int i = 0; i < startIndex-1; i++) {
            listNode = listNode.next;
        }
        int sum = 0;
        int len = endIndex - startIndex;
        while (len>=0){
            sum +=listNode.val;
            listNode = listNode.next;
            len--;
        }

        return sum;
    }

    public ListNode mergeList(ListNode l1, ListNode l2) {
        if(l1 == null){
            return l2;
        }
        if(l2 == null) {
            return l1;
        }
        if(l1.val <= l2.val) {
            l1.next = mergeList(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeList(l1, l2.next);
            return l2;
        }
    }
}
